<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use App\Http\Model\Logs;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class CategoryController extends Controller
{
    /**
     * 管理分类
     * @param $type 分类类型
     * @return $this
     */
    public function manageCategory($type){
        check_auth();
        $categories=Category::where(['type'=>$type])->orderBy('id','asc')->get()->all();
        return view('admin.category.category',compact('type','categories'))->with('title','管理分类');
    }

    /**
     * 添加分类
     * @return array
     */
    public function addCategory(){
        check_auth();
        if($input=Input::all()){
            $category=new Category();
            if($category->create(['category'=>$input['category'],'status'=>$input['status'],'type'=>$input['type']])){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-添加了分类type->'.$input['type'].'分类->'.$input['category']]);
                return ['status'=>1,'msg'=>'添加完成'];
            }else{
                return ['status'=>0,'msg'=>'添加失败'];
            }
        }
    }

    /**
     * 修改分类状态
     * @return array
     */
    public function updateCategoryStatus(){
        check_auth();
        if($input=Input::all()){
            $status=$input['status']?0:1;
            if(Category::where('id',$input['id'])->update(['status'=>$status])){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-更新了分类状态->id:'.$input['id'].'状态->'.$status]);
                return ['status'=>1,'msg'=>'修改完成'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }

    /**
     * 删除分类
     * @return array
     */
    public function deleteCategory(){
        check_auth();
        if($input=Input::all()){
            if(Category::where('id',$input['id'])->delete()){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-删除了一个分类']);
                return ['status'=>1,'msg'=>'修改完成'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }

    /**
     * 更新分类
     * @return array
     */
    public function updateCategory(){
        check_auth();
        if($input=Input::all()){
            $status=$input['status']?1:0;
            if(Category::where('id',$input['id'])->update(['status'=>$status,'category'=>$input['category']])){
                Logs::create(['username'=>Session::get('admin')['username'],'event'=>'更新了分类：'.$input['category']]);
                return ['status'=>1,'msg'=>'修改完成'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }
}
